package com.ricchieri.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.ricchieri.dao.interf.ITripDao;
import com.ricchieri.filter.TripFilter;
import com.ricchieri.model.Trip;
import com.ricchieri.service.interf.ITripService;
import com.ricchieri.util.FacesUtil;

@Transactional(readOnly = true)
public class TripService implements ITripService, Serializable {
	private static final long serialVersionUID = 3772243592804966962L;
	ITripDao tripDao;
	
	@Override
	@Transactional(readOnly = true)
	public List<Trip> getAllTrip() {
		TripFilter tripFilter = new TripFilter();
		return tripDao.getAllTrip(tripFilter);
	}

	public ITripDao getTripDao() {
		return tripDao;
	}

	public void setTripDao(ITripDao tripDao) {
		this.tripDao = tripDao;
	}
	
	@Override
	public Trip getTripById(TripFilter tripFilter) {
		return getTripDao().getTripById(tripFilter);
	}
	
	@Override
	public Map<String, String> getComboTrips() {
		List<Trip> tripsValues = this.getAllTrip();

		HashMap<String, String> trips = new HashMap<String, String>();

		for (Trip tripType : tripsValues) {
			String tripDescription = FacesUtil.getMessage(
					tripType.getPlaceOrigin().getDescription());
			tripDescription = tripDescription.concat(" -> ");
			tripDescription = tripDescription.concat(FacesUtil.getMessage(
					tripType.getPlaceDestiny().getDescription()));

			trips.put(tripDescription, String.valueOf(tripType.getId()));
		}

		return trips;
	}	
	
	@Override
	public List<Trip> getAllTripPaginatedByFilter(TripFilter tripFilter,
			int startRecord){
		return getTripDao().getAllTripPaginatedByFilter(tripFilter, startRecord);
	}

	@Override
	public int getAllTripCountByFilter(TripFilter tripFilter){
		return getTripDao().getAllTripCountByFilter(tripFilter);
	}
}
